<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="news"/>
    <#include "../common/header.ftl">
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/jquery-bootstrap/jquery.bootstrap.min.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <style type="text/css">
        .modal-body {
            max-height: 750px;
            overflow-y: auto;
        }

        .modal-body img {
            width: 100%;
        }
    </style>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">新闻管理</h1>
                </div>
            </div>
        <#--高级查询--->
            <form class="form-inline" id="searchForm" action="/news/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题">
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" id="beginDate" name="beginDate"
                           value="${(qo.beginDate?string("yyyy-MM-dd"))!}"
                           placeholder="请选择开始日期" onclick="WdatePicker({readOnly:true, maxDate:new Date()});">
                    ~
                    <input type="text" class="form-control" id="endDate" name="endDate"
                           value="${(qo.endDate?string("yyyy-MM-dd"))!}"
                           placeholder="请选择结束日期"
                           onclick="WdatePicker({readOnly:true, minDate:$('#beginDate').val(),maxDate:new Date()});">
                </div>
                <div class="form-group">
                    <label for="groupType">新闻类型:</label>
                    <select class="form-control" id="type" name="type">
                        <option value="-1">全部类型</option>
                        <#list types as t>
                            <option value="${t.id}">${t.name}</option>
                        </#list>
                    </select>
                </div>
                <button class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a role="button" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 新增
                </a>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>序号</th>
                    <th>类型</th>
                    <th>标题</th>
                    <th>副标题</th>
                    <th>封面</th>
                    <th>新闻日期</th>
                    <th>操作</th>
                </tr>
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.typeName}</td>
                        <td>${entity.title}</td>
                        <td>${(entity.subTitle)!}</td>
                        <td><img src="${entity.coverUrl}" width="100px;" height="100px;"/></td>
                        <td>${entity.newsTime?string("yyyy-MM-dd")}</td>
                        <td>
                            <a role="button" class="btn btn-info btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>
                            <a role="button" class="btn btn-danger btn-xs deleteBtn"
                               data-url='/news/delete.do?id=${entity.id}'>
                                <span class="glyphicon glyphicon-remove"></span> 删除
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl">
        </div>
    </div>
</div>

<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/news/saveOrUpdate.do" method="post"
                      enctype="multipart/form-data" id="editForm">
                    <input type="hidden" name="id"/>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">新闻类型：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="type">
                                <#list types as t>
                                    <option value="${t.type}">${t.name}</option>
                                </#list>
                            </select>
                            <script>
                                $("select[name=type]").val(${qo.type});
                            </script>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="title"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">副标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="subTitle"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">封面：</label>
                        <div class="col-lg-6">
                            <img id="coverUrl" style="width: 100%"/>
                            <input type="file" class="form-control" name="img"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea name="newsContent.content" id="ckeditor"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<script src="/js/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
    $(function () {
        //初始化一个CKeditor
        var edt = CKEDITOR.replace("ckeditor");

        $(".inputBtn").click(function () {
            $("#editForm input").val("");
            $("#editForm select").val("");
            $("#coverUrl").attr("src", "");

            //处理修改时的数据回显
            var json = $(this).data("json");
            if (json) {
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='title']").val(json.title);
                $("#editForm input[name='subTitle']").val(json.subTitle);
                $("#coverUrl").attr("src", json.coverUrl);
                $("#editForm select[name='type']").val(json.type);
                $.get("/news/getContentById.do", {id: json.id}, function (data) {
                    //文章的内容回显到ckeditor
                    edt.setData(data);
                }, "html");
            } else {
                edt.setData("");
            }
            $("#inputModal").modal("show");
        });
        $(".submitBtn").click(function () {
            //把CKEditor中的数据填充到文本域中,再提交表单
            $("#ckeditor").html(edt.getData());
            $("#editForm").ajaxSubmit(function (data) {
                if (data.success) {
                    location.reload();
                } else {
                    $.messager.alert("温馨提示", data.msg);
                }
            });
        });

    });
</script>
</body>
</html>